home *** CD-ROM | disk | FTP | other *** search
- /*
- * Design Patterns Example
- * © 1997 John Schettino, js12@gte.com
- *
- */
-
- #include <iostream.h>
- #include <string.h>
- // Use STL Stacks and Queues based on Vectors
- #include <list.h>
- #include <stack.h>
-
- // The tree node base class
- typedef class TreeNode * TreeNodePtr;
- typedef class BinaryTreeNode * BinaryTreeNodePtr;
- typedef class TreeNodeIterator * TreeNodeIteratorPtr;
-
- class TreeNode {
- friend ostream& operator<< (ostream& os, TreeNode& t);
- friend ostream& operator<< (ostream& os, const TreeNodePtr& t);
-
- public:
- virtual const char * output(void) {};
- virtual int compare (const TreeNode& otherNode) {};
- };
-
- // A simple binary tree
- class BinaryTreeNode: public TreeNode {
- public:
- BinaryTreeNode(const char *name)
- { _fname = new char[strlen(name)+1];
- strcpy(_fname, name);
- _leftChild = _rightChild = 0; }
- virtual const char* output(void)
- { return _fname; }
- virtual int compare (const BinaryTreeNode& otherNode)
- { return strcmp(_fname, otherNode._fname); }
-
- // you should have a destructor
-
- // these accessors are used by the builder and iterator
- void setLeftChild(const BinaryTreeNodePtr l) { _leftChild = l; }
- void setRightChild(const BinaryTreeNodePtr r) { _rightChild = r; }
- BinaryTreeNodePtr leftChild() { return _leftChild; }
- BinaryTreeNodePtr rightChild() { return _rightChild; }
- private:
- char *_fname;
- BinaryTreeNodePtr _leftChild;
- BinaryTreeNodePtr _rightChild;
- };
-
-
- // Tree Node Iterator base class
- class TreeNodeIterator {
- public:
- // operators
- virtual TreeNodePtr operator() () {}; // get next node
- virtual TreeNodePtr operator++ (int) {};// get next node
- virtual TreeNodePtr operator* () {}; // get current node
-
- // member fn to return the current node only
- virtual TreeNodePtr current() {};
-
- // member fn to move to the next node
- virtual TreeNodePtr next() {};
-
- // member fn to reset iterator to first node
- virtual void reset() {};
- };
-
- // Depth-First BinaryTree Node Iterator
- class DFTreeNodeIterator : public TreeNodeIterator {
- public:
- DFTreeNodeIterator(BinaryTreeNode &tree);
-
- virtual TreeNodePtr operator() () { return operator++(1); }
- virtual TreeNodePtr operator++ (int);
- virtual TreeNodePtr operator* () { return current(); }
- virtual TreeNodePtr current();
- virtual TreeNodePtr next();
- virtual void reset();
- private:
- BinaryTreeNode *_origTree, *_tree;
- // use an STL Stack
- stack<list<BinaryTreeNodePtr> > _pendingNodes;
- void _pushLeft();
-
- };
-
- // Bredth-First BinaryTree Node Iterator
- class BFTreeNodeIterator : public TreeNodeIterator {
- public:
- BFTreeNodeIterator(BinaryTreeNode &tree);
-
- virtual TreeNodePtr operator() () { return operator++(1); }
- virtual TreeNodePtr operator++ (int);
- virtual TreeNodePtr operator* () { return current(); }
- virtual TreeNodePtr current();
- virtual TreeNodePtr next();
- virtual void reset();
- private:
- BinaryTreeNode *_origTree, *_tree;
- // use an STL queue
- queue<list<BinaryTreeNodePtr> > _pendingNodes;
- enum {IO_node, IO_left, IO_right} _current;
- };
-
- // Builder for making trees - base class
- class TreeBuilder {
- public:
- virtual void AddNode(TreeNodePtr theNode) {}
- virtual TreeNodePtr GetTree() { return 0; }
- protected:
- TreeBuilder() {};
- };
-
- // Builder for making binary trees
- class BinaryTreeBuilder : public TreeBuilder {
- public:
- BinaryTreeBuilder();
- virtual void AddNode(TreeNodePtr theNode);
- virtual TreeNodePtr GetTree();
- private:
- TreeNodePtr _currentBTree;
- };
-
- // Builder for making height-balanced binary trees
- class HBTreeBuilder : public TreeBuilder {
- public:
- HBTreeBuilder();
- virtual void AddNode(TreeNodePtr theNode);
- virtual TreeNodePtr GetTree();
- private:
- TreeNodePtr _currentBTree;
- };
-
-
-